// timePicker

(function ($, undefined) {

    // define the name of the UI component
    var ui_name = "timePicker"

    // initialize object
    var tp = $.timePicker = {

        // default values
        defaults: {
            time: new Date(),
            hourList: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
            minuteList: [0, 30]
        },

        setDefaults: function (defaultOptions) {
            this.defaults = $.extend({}, this.defaults, defaultOptions);
        }
    };

    $.fn.timePicker = function (options) {

        // method calling
        if (typeof options == 'string') {
            var args = Array.prototype.slice.call(arguments, 1),
			res;
            this.each(function () {
                var data = $.data(this, ui_name);
                if (data) {
                    var meth = data[options];
                    if (meth) {
                        var r = meth.apply(this, args);
                        if (res === undefined) {
                            res = r;
                        }
                    }
                }
            });
            if (res !== undefined) {
                return res;
            }
            return this;
        }

        // initialize options
        options = $.extend({}, tp.defaults, options);

        this.each(function () {
            var _self = this;

            //initialize
            _initPicker();


            // initialize callback
            for (var key in options) {
                var value = options[key];
                if (typeof value == 'function') {
                    // bind callbacks
                    $(_self).bind(key, value);
                }
            }




            /* Private Methods
            -----------------------------------------------------------------------------*/

            function _initPicker() {
                $(_self)
                  .append('<select class="selHours"></select>时<select class="selMinutes"></select>分');
                var selHours = $('.selHours', _self)[0];
                var selMinutes = $('.selMinutes', _self)[0];

                $(selHours).comboxBindingWithArray(options['hourList']);
                $(selMinutes).comboxBindingWithArray(options['minuteList']);

                $(selHours).change(function () {
                    var currentTime = _getTime();
                    select(currentTime);
                });

                $(selMinutes).change(function () {
                    var currentTime = _getTime();
                    select(currentTime);
                });

            };


            function _getTime() {
                var selHours = $('.selHours', _self);
                var selMinutes = $('.selMinutes', _self);

                var selectedHour = selHours.val();
                var selectedMinute = selMinutes.val();

                var today = new Date();

                var newDate = new Date(
                        today.getFullYear(),
                        today.getMonth(),
                        today.getDate(),
                        selectedHour,
                        selectedMinute,
                        0
                    );

                return newDate;
            };


            /* Callback Triggers
            -----------------------------------------------------------------------------*/


            function select(currentTime) {
                $(_self).trigger('select', currentTime);
            };




            /* Public Methods
            -----------------------------------------------------------------------------*/

            var publicMethods = {
                //set options
                option: function (name, value) {
                    if (value === undefined) {
                        return options[name];
                    }
                    options[name] = value;
                },

                //set time value
                setTime: function (targetTime) {
                    var selHours = $('.selHours', _self);
                    var selMinutes = $('.selMinutes', _self);

                    var targetHour = targetTime.getHours();
                    var targetMinite = targetTime.getMinutes();

                    var minDiff = 1000;
                    var valueInList = '';
                    $('option', selHours).each(function (index, item) {
                        var itemValue = $(item).val();
                        var diff = Math.abs(targetHour - itemValue);
                        if (diff < minDiff) {
                            minDiff = diff;
                            valueInList = itemValue;
                        }
                    });
                    targetHour = valueInList;

                    var minDiff = 1000;
                    var valueInList = '';
                    $('option', selMinutes).each(function (index, item) {
                        var itemValue = $(item).val();
                        var diff = Math.abs(targetMinite - itemValue);
                        if (diff < minDiff) {
                            minDiff = diff;
                            valueInList = itemValue;
                        }
                    });
                    targetMinite = valueInList;

                    selHours.val(targetHour);
                    selMinutes.val(targetMinite);

                },

                //get time value
                getTime: function () {
                    return _getTime();
                }
            };

            $.data(this, ui_name, publicMethods);


        });

        return this;
    };



})(jQuery);
